<?php
/**
 * Created by PhpStorm.
 * User: adminyj
 * Date: 2019/1/24
 * Time: 21:59
 */

namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;
use PHPExcel;
use think\Request;

class PartMember extends Adminbase
{

    /**
     *
     * 党员信息列表
     *
     * */
    public function index()
    {

        if ($this->request->isAjax()) {
            $where = [];
            $order = "level desc,create_time desc";
            $limit = $this->request->param('limit/d', 10);
            $page = $this->request->param('page/d', 10);

            $mobile = input('mobile');
            if($mobile){
                $where[] = ['party_member.mobile','=',$mobile];
            }
            $idCard = input('idCard');
            if($idCard){
                $where[] = ['party_member.card_id','like','%'.$idCard.'%'];
            }

            $list = Db::view('party_member','*')
                ->view('member','username','party_member.user_id = member.id ','left')
                ->where($where)
                ->page($page, $limit)
                ->order($order)
                ->withAttr('sex', function ($value, $data) {
                    if($value == 1){
                        return "男";
                    }else{
                        return "女";
                    }
                })
                ->withAttr('user_id', function ($value, $data) {
                    if(!$value){
                        return "--";
                    }
                })
                ->withAttr('status', function ($value, $data) {
                    if($value == 1){
                        return "正常";
                    }else{
                        return "冻结";
                    }
                })
                ->select();

            $total = Db::view('party_member','*')
                ->view('member','username','party_member.user_id = member.id ','left')
                ->where($where)->count();
            $result = array("code" => 0, "count" => $total, "data" => $list);
            return json($result);
        }
        return $this->fetch();

    }


    /**
     * 导入党员信息
     * */
    public function excelIn()
    {


        $file = request()->file('excel');

        $save_path = ROOT_PATH . 'uploads/';
        $info = $file->move($save_path);
        //print_r($info);exit;

        $filename=$save_path . DIRECTORY_SEPARATOR . $info->getSaveName();
          if(file_exists($filename)) {//如果文件存在
              if( strstr($filename,'.xlsx')) {
                  $PHPReader = new \PHPExcel_Reader_Excel2007();
              }
              else {
                  $PHPReader = new \PHPExcel_Reader_Excel5();
              }

           //载入excel文件
           $PHPExcel = $PHPReader->load($filename);
           $sheet = $PHPExcel->getActiveSheet(0);//获得 sheet
           $highestRow = $sheet->getHighestRow(); // 取得共有数据数
           $data=$sheet->toArray();
           $new_datas = [];
           for($i=4;$i<$highestRow;$i++){
               array_push($new_datas,$data[$i]);
           }

           if(count($new_datas)>0){
               $dataInserts = [];
               foreach ($new_datas as $k => $v){
                   if($v[4] == "男"){
                       $sex = 1;
                   }else{
                       $sex = 2;
                   }
                   $dataInsert = array(
                       'real_name' => $v[1],
                       'zhibu' => $v[2],
                       'card_id' => $v[3],
                       'sex' => $sex,
                       'nation' => $v[5],
                       'bron_date' => $v[6],
                       'educational' => $v[7],
                       'person_type' => $v[8],
                       'rudang_date' => $v[9],
                       'zhuanzheng_date' => $v[10],
                       'work_place' => $v[11],
                       'mobile' => $v[12],
                       'phone' => $v[13],
                       'address' => $v[14],
                       'status' => $v[15]
                   );
                   $mobile = $v[12];
                   $uid = Db::name('member')->where(['mobile' => $mobile])->value('id');
                   if($uid){
                       $dataInsert['user_id'] = $uid;
                   }
                   $isPart = Db::name('party_member')->where(['mobile' => $mobile])->value('id');
                   if(!$isPart){
                       array_push($dataInserts,$dataInsert);
                   }
               }

               $res = Db::name('party_member')->insertAll($dataInserts, true);
               if($res){
                   $this->success('上传成功' . count($dataInserts) . '条数据');
               }else{
                   $this->error('上传失败');
               }
           }else{
               $this->success('内容为空');
           }


     // return $ret;
    }else{
        return array("resultcode" => -5, "resultmsg" => "文件不存在", "data" => null);
    }

    }


    /**
     * 党员信息添加/修改页面
     * */
    public function add()
    {

        if($this->request->isPost()){

            $where = [];
            $map = [];
            $add = true;
            $user_id = 0;
            $data = input('post.');

            if(!$data){
                $this->error('数据提交有误');
            }

            if(!empty($data['id'])){
                $add = false;
                $id = $data['id'];
                unset($data['id']);
            }

            if($add){

                $where[] = ['mobile','=',$data['mobile']];
                $map[] = ['card_id','=',$data['card_id']];

                $count = Db::name('party_member')->where($where)->whereOr($map)->count();

                if($count > 0){
                    $this->error('该党员信息已录入');
                }else{
                    $res = Db::name('party_member')->insertGetId($data);
                    $user_id = $res;
                }

            }else{
                $res = Db::name('party_member')->where(['id' => $id])->update($data);
            }

            $res !== false ? true : false;
            if($res){
                $user_id = Db::name('member')->where(['mobile' => $data['mobile']])->value('id');
                if($user_id){
                    $updateData['user_id'] = $user_id;
                    Db::name('party_member')->where(['id' => $id])->update($updateData);
                }

                $this->success('操作成功');
            }else{
                $this->error('操作失败');
            }


        }else{

            $id = $this->request->param('id/d');

            $data = Db::name('party_member')->where(['id' => $id])->find();

            $this->assign('data',$data);

            return $this->fetch();
        }

    }



    /**
     * 党员信息修改页面
     * */
    public function edit()
    {



    }

    /**
     * 党员信息删除
     * */
    public function del()
    {

        $id = input('id');
        if(!$id){
            $this->error('数据获取有误');
        }

        $where[] = ['id','=',$id];

        $res = Db::name('party_member')->where($where)->delete();

        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }

}